package com.powernobug.servlet;

import com.powernobug.bean.model.MarketAd;
import com.powernobug.bean.model.MarketCoupon;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.param.AdQueryParam;
import com.powernobug.bean.vo.param.BaseQueryParam;
import com.powernobug.service.AdminAdService;
import com.powernobug.service.impl.AdminAdServiceImpl;
import com.powernobug.util.BeanUtil;
import com.powernobug.util.IOUtil;
import com.powernobug.util.JacksonUtil;
import com.powernobug.util.JsonUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

/**
 * @description:
 * @project: project1-no-bug
 * @package: com.powernobug.servlet
 * @author: HuangWeiLong
 * @date: 2024/8/23 9:10
 */
@WebServlet("/ad/*")
@MultipartConfig
public class AdminAdServlet extends CommonServlet{
    static {
        DateConverter dateConverter = new DateConverter(null);
        dateConverter.setPatterns(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
        ConvertUtils.register(dateConverter, Date.class);
    }
    AdminAdService adminAdService=new AdminAdServiceImpl();
    public BaseRespVo list(HttpServletRequest request){
        AdQueryParam adQueryParam = BeanUtil.queryParamPagemant(AdQueryParam.class, request);
        PageVo data=adminAdService.list(adQueryParam);
        return BaseRespVo.ok(data);
    }
    public BaseRespVo create(HttpServletRequest request) throws IOException, ServletException {
        String json = request.getReader().readLine();
        Map map = JacksonUtil.read(json, Map.class);
        MarketAd marketAd = new MarketAd();
        try {
            BeanUtils.copyProperties(marketAd,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminAdService.create(marketAd);
    }
    public BaseRespVo update(HttpServletRequest request) throws IOException {
        String jrson = request.getReader().readLine();
        Map map = JacksonUtil.read(jrson, Map.class);

        MarketAd marketAd = new MarketAd();
        try {
            BeanUtils.copyProperties(marketAd,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return adminAdService.update(marketAd);
    }

    public BaseRespVo delete(HttpServletRequest request) throws IOException {
        String jrson = request.getReader().readLine();
        Map map = JacksonUtil.read(jrson, Map.class);

        MarketAd marketAd = new MarketAd();
        try {
            BeanUtils.copyProperties(marketAd,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        adminAdService.delete(marketAd);
        return BaseRespVo.ok(null);
    }
}
